<template>
  <el-container>
    <el-header class="no-flex" height="auto">
      <el-form size="default" inline label-width="80px" :model="searchForm">
        <el-form-item label="字典名称" class="inline">
          <el-select v-model="this.searchForm.dictType" class="m-2" placeholder="请选择字典名称" @change="select">
            <el-option
              v-for="item in dictList"
              :label="item.name"
              :key="item.type"
              :value="item.type"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="字典键" class="inline" >
          <el-input v-model="searchForm.key" placeholder="请输入字典键" :clearable="true" style="width: 200px"></el-input>
        </el-form-item>
        <el-form-item label="字典值" class="inline">
          <el-input v-model="searchForm.value" placeholder="请输入字典值" :clearable="true" style="width: 200px"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" icon="el-icon-search"  @click="upsearch">
            <span>搜索</span>
          </el-button>
          <el-button type="default" icon="el-icon-RefreshLeft" @click="reset">
            <span>重置</span>
          </el-button>
        </el-form-item>
        <el-form-item style="display: flex; margin-bottom: 0">
          <el-button type="primary" icon="el-icon-plus" @click="add(this.typeId)">新增</el-button>
        </el-form-item>

      </el-form>
    </el-header>
    <el-main class="nopadding">
      <el-table ref="table" @selection-change="selectionChange" :data="tableData">
        <el-table-column type="selection" width="50"></el-table-column>
        <el-table-column label="字典键名" prop="k" width="150"></el-table-column>
        <el-table-column label="字典键值" prop="v" width="150"></el-table-column>
        <el-table-column label="排序" prop="sort" width="150"></el-table-column>
        <el-table-column label="字典描述" prop="remark" width="100"></el-table-column>
        <el-table-column label="创建时间" prop="createTime" :formatter="rendererDateTime" min-width="200"></el-table-column>
        <el-table-column label="操作" fixed="right" align="right" width="200">
          <template #default="scope">
            <el-button-group>
              <el-button text type="primary"  v-if="scope.row.system != 1" @click="table_edit(scope.row.id,this.typeId)" icon="el-icon-Edit" v-auths="['*:*:*','system:dicttype:update']">编辑</el-button>
              <el-popconfirm title="确定删除吗？" v-if="scope.row.system != 1" @confirm="table_del(scope.row, scope.$index)">
                <template #reference>
                  <el-button text type="danger" icon="el-icon-delete" v-auths="['*:*:*','system:dicttype:delete']">删除</el-button>
                </template>
              </el-popconfirm>
            </el-button-group>
          </template>
        </el-table-column>
      </el-table>
    </el-main>
    <el-footer style="display: flex;justify-content: flex-end">
      <el-pagination
        :current-page="searchForm.current"
        :page-size="searchForm.size"
        :page-sizes="[10, 20, 50, 100]"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
      />
    </el-footer>
  </el-container>
  <save-dialog v-if="dialog.save" ref="saveDialog" @success="handleSaveSuccess" @closed="dialog.save=false"></save-dialog>

</template>

<script>
import saveDialog from './components/item-save.vue'
export default {
  name: 'dict-item',
  components: {
    saveDialog
  },
  data() {
    return {
      dialog: {
        save: false
      },
      total:0,
      name:null,
      dictList:[],
      // currentDictType: {},
      typeId:this.$route.query.id,
      searchForm:{
        current:1,
        size: 10,
        dictType: this.$route.query.type,
        key:'',
        value:''
      },
      selection: [],
      tableData:[],
    }
  },
  created() {
    this.getData()
  },
  methods: {
    //切换分页
    handleCurrentChange(e){
      this.searchForm.current = e;
      this.getData()
    },
    // 改变单页显示数量
    handleSizeChange(e){
      this.searchForm.size = e
      this.getData()
    },
    //选择不同字典项
    select(e){
      this.searchForm.dictType=e
      // this.dictList.forEach((item) => {
      //   if(e === item.type){
      //     this.currentDictType = item
      //   }
      // })
    },
    // 请求数据
    getData(){
      this.$API.system.dictItem.list.get(this.searchForm).then(res=>{
        this.tableData  = res.data.records
        this.total = res.data.total
        })
      this.$API.system.dict.list.get({current:1,size:100}).then(res=>{
        this.dictList  = res.data.records
        this.dictList.forEach((item) => {
          if(item.type === this.searchForm.dictType){
            this.typeId = item.id
          }
        })
      })
    },
    // 转换时间
    rendererDateTime(row, column, cellValue, index){
      return this.$TOOL.dateFormat(cellValue)
    },
    //添加
    add(typeId){
      this.dialog.save = true
      this.$nextTick(() => {
        this.$refs.saveDialog.open().setData(null,typeId)
      })
    },
    //编辑
    table_edit(id,typeId){
      this.dialog.save = true
      this.$nextTick(() => {
        this.$refs.saveDialog.open('edit').setData(id,typeId)
      })
    },
    //查看
    table_show(row){
      this.dialog.save = true
      this.$nextTick(() => {
        this.$refs.saveDialog.open('show').setData(row)
      })
    },
    //删除
    async table_del(row){
      let res = await this.$API.system.dictItem.delItem.get(row.id)
      if(res.code == 200){
        this.$message.success('删除成功')
        this.tableData = []
        this.getData()
      } else{
        this.$message.error('删除失败')
      }
    },
    //表格选择后回调事件
    selectionChange(selection){
      this.selection = selection;
    },
    //搜索
    async upsearch(){
      this.getData()
    },
    //重置功能
    reset(){
      this.searchForm.key=''
      this.searchForm.value=''
      this.tableData=[];
      this.getData()
    },

    //本地更新数据
    handleSaveSuccess(data, mode){
      this.tableData=[]
      this.dictList=[]
      this.getData()
    }
  }
}
</script>

<style scoped>

</style>
